package cloud.maque.base.dingding.impl;

import cloud.maque.base.dingding.hander.DingdingTokenServiceHander;
import cloud.maque.base.dingding.response.WorkMessageResponse;
import cloud.maque.base.dingding.resquest.DingDingMessageBody;
import cloud.maque.base.dingding.resquest.WorkMessageRequest;
import cloud.maque.base.dingding.service.DingDingImNotificationService;
import cloud.maque.framework.conf.PlatformConfigProperties;
import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhuyf
 * @date 2023/3/3 12:38
 */
@Service
@Slf4j
public class DingDingImNotificationServiceImpl implements DingDingImNotificationService {

    @Autowired
    PlatformConfigProperties properties;

    @Autowired
    DingdingTokenServiceHander tokenServiceHander;

    @Override
    public boolean SendWorkMessage(WorkMessageRequest workMessageRequest) {

        String accessToken = tokenServiceHander.GetAccessEnterpriseToken();
        DingTalkClient client = new DefaultDingTalkClient(properties.getDingding().getApiUrl() + "/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
        req.setAgentId(properties.getDingding().getAgentId());
        req.setToAllUser(workMessageRequest.isToAllUser());
        if (workMessageRequest.getDeptids() != null) {
            req.setDeptIdList(String.join(",", workMessageRequest.getDeptids()));
        }
        if (workMessageRequest.getUserids() != null) {
            req.setUseridList(String.join(",", workMessageRequest.getUserids()));
        }

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = null;

        switch (workMessageRequest.getMsg().getMessageType()) {
            case text:
                msg = SetText(workMessageRequest.getMsg());
                break;
            default:
                throw new RuntimeException("未实现");

        }
        req.setMsg(msg);

        try {
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, accessToken);
            WorkMessageResponse workMessageResponse = JSON.parseObject(rsp.getBody(), WorkMessageResponse.class);
            if (!workMessageResponse.getErrmsg().equalsIgnoreCase("ok")) {
                throw new RuntimeException(workMessageResponse.getErrmsg());
            }

        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        return true;
    }

    private OapiMessageCorpconversationAsyncsendV2Request.Msg SetText(DingDingMessageBody msgBody) {
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype(msgBody.getMessageType().name());
        msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
        msg.getText().setContent(msgBody.getText().getContent());
        return msg;
    }
}
